package com.wavehi.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author WaveHi
 * @Date 23/9/2021 上午 9:10
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
public class BaseServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //统一字符集
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("UTF-8");
        //获取action参数值
        String action = req.getParameter("action");
        //验证action的值
        boolean isCorrect = action != null && !"".equals(action.trim());
        //调用相应的业务方法
        if(isCorrect) {
            try {
                //获取方法
                Method actionMethod = this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
                //开放访问级别
                actionMethod.setAccessible(true);
                //调用方法
                actionMethod.invoke(this, req, resp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
